// import createPersistedState from "vuex-persistedstate";

import Vue from "vue";
import Vuex from "vuex";
import getters from "./getters";
import app from "./modules/app";
import settings from "./modules/settings";
import wages from "./modules/wages";
import recruitment from "./modules/recruitment";
import check from "./modules/check";
import clientele from "./modules/clientele";
import hr from "./modules/hr";
import company from "./modules/company";
import navtab from "./modules/navtab";
import admin from "./modules/admin";
import Table from "./modules/Table";
import taxCalculate from "./modules/taxCalculate";
import site from "./modules/site";
import enums from "./modules/enums";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    loading: false,
    reqCount: 0
  },
  modules: {
    enums,
    app,
    settings,
    wages,
    recruitment,
    check,
    clientele,
    navtab,
    hr,
    company,
    admin,
    Table,
    taxCalculate,
    site
  },
  getters,
  actions: {
    onCompanyClick({ dispatch, commit }, payload) {
      commit("hr/updateCompany", payload);
      if (payload.id && payload.name) {
        commit("site/updateCurrentItems", payload);
      } else {
        dispatch("site/updateCurrentItemsAct");
      }
    },
    onCompanyListUpdate({ dispatch, commit, rootState }) {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve, reject) => {
        const loadRes = await dispatch("hr/loadCompanyList");
        if (loadRes === true) {
          commit("site/initCurrentItems", rootState.hr.companyList);
          resolve(true);
        } else {
          reject(false);
        }
      });
    }
  }
  // plugins: [
  //   createPersistedState({
  //     storage: window.localStorage
  //   })
  // ]
});

export default store;
